/*
 * @创建文件时间: 2021-06-03 00:50:28
 * @Auther: 猿小天
 * @最后修改人: 猿小天
 * @最后修改时间: 2021-11-29 10:46:51
 * 联系Qq:1638245306
 * @文件介绍: 菜单的按钮和接口配置
 */
import { BUTTON_WHETHER_BOOL } from '@/config/button'
import util from '@/libs/util'
const uploadUrl = util.baseURL() + 'api/system/file/'
export const crudOptions = (vm) => {
  return {
    pageOptions: {
      compact: true
    },
    options: {
      tableType: 'vxe-table',
      rowKey: false,
      width: '100%',
      height: '100%' // 表格高度100%, 使用toolbar必须设置
    },
    rowHandle: {
      width: 160,
      view: {
        thin: true,
        text: ''
      },
      edit: {
        thin: true,
        text: ''
      },
      remove: {
        thin: true,
        text: ''
      }
    },
    indexRow: { // 或者直接传true,不显示title，不居中
      title: '序号',
      align: 'center',
      width: 100
    },
    viewOptions: {

      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 12, // 默认的表单 span
      labelWidth: '130px'
    },
    columns: [{
      title: '关键词',
      key: 'search',
      show: false,
      disabled: true,
      search: {
        disabled: false
      },
      form: {
        disabled: true
      },
      view: { // 查看对话框组件的单独配置
        disabled: true
      }
    },
    {
      title: 'ID',
      key: 'id',
      show: false,
      width: 90,
      form: {
        disabled: true
      }
    },

    {
      title: '软件标识(appid)',
      key: 'app_id',
      search: {
        disabled: true
      },
      type: 'input',
      form: {
        value: vm.$route.query.app_id,
        component: {
          show: false,
          placeholder: '请输入软件标识',
          props: {
            clearable: true
          }
        },
        rules: [ // 表单校验规则
          { required: true, message: '请输入软件标识' }
        ],
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },

    {
      title: '升级类型',
      key: 'upgrade_type',
      search: {
        disabled: true
      },
      type: 'select',
      dict: {
        data: [
          { label: '整包', value: 0 },
          { label: '部分', value: 1 }
        ]
      },
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '必填项' }
        ],
        component: {
          span: 12

        },

        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '更新标题',
      key: 'title',
      search: {
        disabled: false
      },
      type: 'input',
      show: false,
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '必填项' }
        ],
        component: {
          span: 12,
          placeholder: '请输入更新标题'
        },

        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '更新内容',
      key: 'content',
      search: {
        disabled: true
      },
      type: 'textarea',
      show: false,
      form: {
        rules: [ // 表单校验规则
          { max: 1000, message: '最大输入1000，请重新输入', trigger: 'blur' },
          { required: true, message: '必填项' }
        ],
        component: {
          span: 24,
          placeholder: '请输入更新内容',
          showWordLimit: true,
          props: {
            type: 'textarea',
            autosize: {
              minRows: 2, maxRows: 8
            }
          }
        },

        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '应用平台',
      key: 'platform',
      search: {
        disabled: false
      },
      type: 'select',
      dict: {
        data: [
          { label: 'Windows', value: 0 },
          { label: 'Linux', value: 1 }
        ]
      },
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '必填项' }
        ],
        component: {
          span: 12,
          placeholder: '请选择应用平台'

        },

        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '版本号',
      key: 'version',
      search: {
        disabled: false
      },
      type: 'input',
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '必填项' },
          { pattern: /^([0-9]\d|[0-9])(\.([0-9]\d|\d)){2}$/, message: '请输入正确的版本号' }
        ],
        component: {
          span: 12,
          placeholder: '请输入版本号'

        },

        itemProps: {
          class: { yxtInput: true }
        },
        helper: '版本号请以x.x.x的形式'
      }
    },
    {
      title: '上传文件',
      key: 'file',
      type: 'file-uploader',
      show: false,
      form: {

        component: {
          props: {
            uploader: {
              action: uploadUrl,
              name: 'url',
              headers: {
                Authorization: 'JWT ' + util.cookies.get('token')
              },
              type: 'form',
              successHandle (ret, option) {
                if (ret.data == null || ret.data === '') {
                  throw new Error('上传失败')
                }
                return { url: ret.data.data.id, key: option.data.key }
              }
            },
            elProps: { // 与el-uploader 配置一致
              multiple: false,
              limit: 1 // 限制5个文件
            },
            sizeLimit: 200000 * 1024 // 不能超过限制
          },
          span: 24
        },
        valueChange (key, value, form, { getComponent }) {
          form.file_url = value
        },
        helper: '限制文件大小不能超过200M'
      },
      valueResolve (row, col) {
        const value = row[col.key]
        if (value != null && value instanceof Array) {
          if (value.length >= 0) {
            row[col.key] = value[0]
          } else {
            row[col.key] = null
          }
        }
      },
      component: {
        props: {
          buildUrl (value, item) {
            if (value && value.indexOf('http') !== 0) {
              return '/api/upload/form/download?key=' + value
            }
            return value
          }
        }
      }
    },
    {
      title: '包地址',
      key: 'file_url',
      search: {
        disabled: true
      },
      type: 'input',
      show: false,
      form: {
        disabled: true,
        rules: [ // 表单校验规则
          { required: true, message: '必填项' }
        ],
        component: {
          span: 24,
          placeholder: '请输入包地址'
        },

        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '强制更新',
      key: 'coerce_upgrade',
      type: 'dict-switch',
      show: false,
      search: {
        disabled: true
      },
      dict: {
        data: BUTTON_WHETHER_BOOL
      },
      form: {
        value: false,
        component: {

        }
      }
    },
    {
      title: '上线发行',
      key: 'status',
      search: {
        disabled: true
      },
      type: 'dict-switch',
      dict: {
        data: [
          {
            label: '上线',
            value: true,
            color: 'success'
          },
          {
            label: '下线',
            value: false,
            color: 'danger'
          }
        ]
      },
      form: {
        value: true,
        component: {
          placeholder: '请选择上线发行'
        }
      }
    }
    ]
  }
}
